from datetime import datetime
from django.contrib.auth import authenticate, login as _login, logout as _logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User
from django.shortcuts import render,reverse,redirect
from django.http import HttpResponse
from django.contrib import  messages
from commodity.models import  Commodity,CommodityCategory
# Create your views here.
from account.models import UserMoney, MoneyChangeRecord


def register(request):
    '''
    注册
    :param request:
    :return:
    '''
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        if username and password:
            if User.objects.get(username=username).exists():
                messages.info(request,'该用户已存在')
                return redirect(reverse('account:register'))
            else:
                User.objects.create_user(username=username, password=password)
                messages.info(request, '注册成功，请登录！')
                return redirect(reverse("account:login"))
        else:
            messages.info(request,'用户名或密码不能为空')
            return redirect(reverse('account:register'))
    return render(request,'account/register.html')


def login(request):
    '''
    用户登录
    :param request:
    :return:
    '''
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        user = authenticate(username=username, password=password)
        if user:
            _login(request, user)
            return redirect(reverse('account:index'))
        else:
            messages.info(request,'用户名或密码错误')
            return redirect(reverse('account:login'))
    else:
        if request.user.username:
            redirect(reverse('account:index'))
        return render(request,'account/login.html')


@login_required
def index(request):
    '''
    商品显示、充值
    :param request:
    :return:
    '''
    if request.method == "POST":
        balance = request.POST.get('balance')
        user = UserMoney.objects.filter(user=request.user)
        if balance is not None:
            if user.exists():
                user =UserMoney.objects.get(user=request.user)
                user.balance += int(balance)
                user.save()
            else:
                UserMoney.objects.create(user=request.user,balance=int(balance))
            order_no = int(datetime.now().timestamp())
            MoneyChangeRecord.objects.create(user=request.user,change_money=int(balance),order_no=order_no)
            messages.success(request,'充值成功')
            return redirect(reverse('account:index'))
        messages.error(request,'充值失败')
        return redirect(reverse('account:index'))
    commodity_list = Commodity.objects.all()
    return render(request,'account/index.html',locals())


def logout(request):
    '''
    注销用户
    :param request:
    :return:
    '''
    _logout(request)
    return redirect(reverse('account:login'))


@login_required
def change_password(request):
    '''
    修改密码
    :param request:
    :return:
    '''
    if request.method == "POST":
        username =request.user.username
        user = User.objects.get(username=username)
        getpwd = user.password
        password = request.POST.get("password")
        newpassword = request.POST.get('newpassword')
        oddpassword = request.POST.get('oddpassword')
        if check_password(oddpassword,getpwd):
            if password == newpassword and password != '':
                user.set_password(password)
                user.save()
                messages.info(request,'密码修改成功，请重新登录')
                return redirect(reverse('account:login'))
            messages.info(request,'密码不能为空或者两次输入不一致，请重新输入')
        else:
            messages.info(request,'旧密码输入错误，请重新输入')
            return redirect(reverse('account:change_password'))
    return render(request,'account/change_password.html')


@login_required
def recharge(request):
    '''
    充值页面
    :param request:
    :return:
    '''
    return render(request,'account/recharge.html')


@login_required
def recharge_history(request):
    '''
    充值记录
    :param request:
    :return:
    '''
    recharge_list = MoneyChangeRecord.objects.filter(change_type=0).order_by('-change_time')
    return render(request,'account/recharge_history.html',locals())


@login_required
def order_record(request):
    '''
    订单记录
    :param request:
    :return:
    '''
    return render(request,'account/order_record.html',locals())
